package com.android.chessgame;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<ModelList> list;

    public interface OnItemClickListener{

        void onItemClick(View view,int position);

    }
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public MyAdapter(Context context, List<ModelList> list){
        this.mContext = context;
        this.list =list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.recycler_item,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//            Log.e("onBindViewHolder","pos="+position);
            final MyHolder myHolder = (MyHolder) holder;
            myHolder.ivMap.setImageResource(list.get(position).getMapImage());
            if (list.get(position).getPieceType()==0){
                myHolder.ivPiecesImage.setImageResource(-1);
            }else {
                myHolder.ivPiecesImage.setImageResource(list.get(position).getPieceImage());
            }

            if (list.get(position).isSel()){
                myHolder.ivPiecesImageSel.setVisibility(View.VISIBLE);
            }else {
                myHolder.ivPiecesImageSel.setVisibility(View.GONE);
            }


        myHolder.itemView.setOnClickListener(v -> {
//                Toast.makeText(mContext,"选中了第"+(position+1)+"个",Toast.LENGTH_SHORT).show();
            if (onItemClickListener!=null){
                onItemClickListener.onItemClick(myHolder.itemView,position);
            }
        });
}

    @Override
    public int getItemCount() {
        return list.size();
    }


    class MyHolder extends RecyclerView.ViewHolder{
        ImageView ivMap;
        ImageView ivPiecesImage;
        ImageView ivPiecesImageSel;
        public MyHolder(View itemView) {
            super(itemView);
            ivMap = (ImageView) itemView.findViewById(R.id.ivMap);
            ivPiecesImage = (ImageView) itemView.findViewById(R.id.ivPiecesImage);
            ivPiecesImageSel = (ImageView) itemView.findViewById(R.id.ivPiecesImageSel);
        }
    }



}
